home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2a.lha / p4-1.2a / lib / p4_sock_cr.c < prev    next >
C/C++ Source or Header  |  1992-12-16  |  6KB  |  245 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. int create_remote_processes(pg)
  5. struct p4_procgroup *pg;
  6. {
  7.     struct p4_procgroup_entry *pe;
  8.     int i, rm_num = 1;
  9.  
  10.     for (i = 0, pe = pg->entries; i < pg->num_entries; i++, pe++)
  11.     {
  12.     if (strcmp(pe->host_name, "local") != 0)
  13.     {
  14.         create_one_remote(pe, rm_outfile_head, rm_num++);
  15.     }
  16.     }
  17.  
  18.     return (0);
  19. }
  20.  
  21. P4VOID create_one_remote(pe, outfile, rm_num)
  22. struct p4_procgroup_entry *pe;
  23. char *outfile;
  24. int rm_num;
  25. {
  26.     int rm_fd;
  27.     struct bm_rm_msg msg;
  28.     P4BOOL done;
  29.     int type;
  30.     int status, port, remote_switch_port;
  31.     int slave_idx, slave_pid, pidx, rm_ind;
  32.  
  33.     rm_fd = net_create_slave(pe->host_name,
  34.                  pe->slave_full_pathname,
  35.                  pe->username);
  36.  
  37.     msg.type = p4_i_to_n(INITIAL_INFO);
  38.     msg.numinproctab = p4_i_to_n(p4_global->num_in_proctable);
  39.     msg.numslaves = p4_i_to_n(pe->numslaves_in_group);
  40.     strcpy(msg.outfile, outfile);
  41.     msg.rm_num = p4_i_to_n(rm_num);
  42.     msg.debug_level = p4_i_to_n(remote_debug_level);
  43.     msg.memsize = p4_i_to_n(globmemsize);
  44.     msg.logging_flag = p4_i_to_n(logging_flag);
  45.     strcpy(msg.application_id, p4_global->application_id);
  46.     strcpy(msg.version, p4_version());
  47.     strcpy(msg.pgm, pe->slave_full_pathname);
  48.  
  49.     net_send(rm_fd, &msg, sizeof(msg), FALSE);
  50.  
  51.     port = -1;
  52.     pidx = -1;
  53.     for (done = FALSE; !done;)
  54.     {
  55.     status = net_recv(rm_fd, &msg, sizeof(msg));
  56.     if (status == PRECV_EOF)
  57.     {
  58.         p4_dprintf("OOPS! got EOF in create_one_remote\n");
  59.         return;
  60.     }
  61.  
  62.  
  63.     type = p4_n_to_i(msg.type);
  64.     switch (type)
  65.     {
  66.       case REMOTE_LISTENER_INFO:
  67.         port = p4_n_to_i(msg.port);
  68.         break;
  69.  
  70.       case REMOTE_MASTER_INFO:
  71.       case REMOTE_SLAVE_INFO:
  72.         if (type == REMOTE_MASTER_INFO)
  73.            rm_ind = TRUE;
  74.         else
  75.            rm_ind = FALSE;
  76.         slave_idx = p4_n_to_i(msg.slave_idx);
  77.         slave_pid = p4_n_to_i(msg.slave_pid);
  78.         remote_switch_port = p4_n_to_i(msg.switch_port);
  79.         if (port == -1)
  80.         p4_dprintf("OOPS! got slave_info w/o getting port first\n");
  81.         /* big master installing remote processes */
  82.         pidx = install_in_proctable(rm_num,port,slave_pid,
  83.                     msg.host_name,slave_idx,
  84.                     msg.machine_type,remote_switch_port);
  85.             p4_dprintfl(90, "create_one_remote: adding connection: %d --> %d\n",
  86.                 p4_get_my_id(), pidx);
  87.  
  88.             if (p4_local->conntab[pidx].type == CONN_REMOTE_SWITCH)
  89.             {
  90.             p4_local->conntab[pidx].switch_port = remote_switch_port;
  91.             p4_local->conntab[pidx].port = rm_fd;
  92.             }
  93.             else if (p4_local->conntab[pidx].type == CONN_REMOTE_NON_EST)
  94.             {
  95.         if (type == REMOTE_MASTER_INFO)
  96.         {
  97.                 p4_local->conntab[pidx].type = CONN_REMOTE_EST;
  98.                 p4_local->conntab[pidx].port = rm_fd;
  99.             p4_local->conntab[pidx].same_data_rep =
  100.             same_data_representation(p4_local->my_id,pidx);
  101.         }
  102.             }
  103.             else
  104.             {
  105.             p4_error("create_one_remote: invalid conn type in conntab\n",
  106.                  p4_local->conntab[pidx].type);
  107.             }
  108.         break;
  109.  
  110.       case REMOTE_SLAVE_INFO_END:
  111.         done = TRUE;
  112.         break;
  113.     }
  114.     }
  115.  
  116. }
  117.  
  118.  
  119. /*
  120.  *    Run the slave pgm on host; returns the file descriptor of the
  121.  *    connection to the slave.
  122.  */
  123. int net_create_slave(host, pgm, username)
  124. char *host;
  125. char *pgm;
  126. char *username;
  127. {
  128.     int serv_port, serv_fd;
  129.     int slave_fd;
  130.     int slave_pid, master_pid;
  131.     char hostname[100];
  132.     struct net_initial_handshake hs;
  133.     char myhost[100];
  134.     int connection_fd, success, rc;
  135.     struct net_message_t msg;
  136.     char remote_shell[64];
  137.     char serv_port_c[64];
  138.     char *am_slave_c = "-amp4slave";
  139. #if defined(SYMMETRY) || defined(SUN) || \
  140.     defined(DEC5000)  || defined(SGI) || \
  141.     defined(RS6000)
  142.     char *getpw();
  143. #endif
  144.  
  145.     net_setup_anon_listener(5, &serv_port, &serv_fd);
  146.  
  147.     myhost[0] = '\0';
  148.     get_qualified_hostname(myhost);
  149.  
  150.     /* try to connect to (secure) server */
  151.  
  152. #if defined(SYMMETRY) || defined(SUN) || \
  153.     defined(DEC5000)  || defined(SGI) || \
  154.     defined(RS6000)
  155.  
  156.     /*****  secure server stuff  *******/
  157.     p4_dprintfl(20, "trying to create remote slave on %s via server\n",host);
  158.     rc = start_slave(host, username, pgm, serv_port, am_slave_c, getpw);
  159.  
  160.     if (rc < -1)
  161.     {
  162.     extern char *start_prog_error;
  163.  
  164.     p4_dprintf("Error with secure server: %s\n", start_prog_error);
  165.     }
  166.     else if (rc == 0)
  167.         p4_dprintfl(10, "created remote slave on %s via server\n",host);
  168.     /*****************************************/
  169. #else
  170.     rc = -1;
  171. #endif
  172.  
  173.     if (rc <= -1)
  174.     {
  175.     /* try to connect to (old) server */
  176.     connection_fd = net_conn_to_listener(host, UNRESERVED_PORT, 1);
  177.  
  178.     if (connection_fd >= 0)
  179.     {
  180.         p4_dprintfl(20, "creating remote slave on %s via old server\n",host);
  181.         msg.type = p4_i_to_n(NET_EXEC);
  182.         strcpy(msg.pgm, pgm);
  183.         strcpy(msg.host, myhost);
  184.         strcpy(msg.am_slave, am_slave_c);
  185.         msg.port = p4_i_to_n(serv_port);
  186.         net_send(connection_fd, &msg, sizeof(msg), FALSE);
  187.         net_recv(connection_fd, &msg, sizeof(msg));
  188.  
  189.         success = p4_n_to_i(msg.success);
  190.         if (!success)
  191.         {
  192.         p4_dprintf("create failed: %s\n", msg.message);
  193.         return (-1);
  194.         }
  195.         close(connection_fd);
  196.         p4_dprintfl(10, "created remote slave on %s via old server\n",host);
  197.     }
  198.     else
  199.     {
  200. #if defined(DELTA)
  201.         p4_dprintf("delta cannot create remote processes\n");
  202. #else
  203.         p4_dprintfl(20, "creating remote slave on %s via remote shell\n",host);
  204. #ifdef P4BSD
  205.         strcpy(remote_shell, "rsh");
  206. #endif
  207.  
  208. #ifdef P4SYSV
  209. #    if defined(TITAN) || defined(SGI)
  210.         strcpy(remote_shell, "rsh");
  211. #    else
  212. #        if defined(SYMMETRY_PTX)
  213.         strcpy(remote_shell, "resh");
  214. #        else
  215.         strcpy(remote_shell, "remsh");
  216. #        endif
  217. #    endif
  218. #endif
  219.  
  220.         sprintf(serv_port_c, "%d", serv_port);
  221.         rc = fork_p4();
  222.         if (rc == 0)
  223.         {
  224.         rc = execlp(remote_shell, remote_shell,
  225.                 host, "-l", username, "-n", pgm,
  226.                 myhost, serv_port_c, am_slave_c, 0);
  227.         /* host,"-n","cluster","5",pgm,myhost,serv_port_c,0); for butterfly */
  228.         if (rc < 0)
  229.             p4_error("net_create_slave: execlp", rc);
  230.         }
  231.         p4_dprintfl(10, "created remote slave on %s via remote shell\n",host);
  232. #endif
  233.     }
  234.     }
  235.     slave_fd = net_accept(serv_fd);
  236.     close(serv_fd);
  237.  
  238.     hs.pid = (int) htonl(getpid());
  239.     net_send(slave_fd, &hs, sizeof(hs), FALSE);
  240.     net_recv(slave_fd, &hs, sizeof(hs));
  241.     slave_pid = (int) ntohl(hs.pid);
  242.  
  243.     return slave_fd;
  244. }
  245.